package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.FittingProduct;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetFittingItem extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetFittingItem(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<FittingProduct> getData(String fittingId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(fittingId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
//        HashMap map = new HashMap();
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<FittingProduct> fittingProducts = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                FittingProduct fitting = new FittingProduct();
                System.err.println(i);
                fitting.setProductValue(jsonArray.getJSONArray(i).getString(0));
                fitting.setColor(jsonArray.getJSONArray(i).getString(1));
                fitting.setSize(jsonArray.getJSONArray(i).getString(2));
                fitting.setPriceList(jsonArray.getJSONArray(i).getString(3));
                fitting.setQty(jsonArray.getJSONArray(i).getString(4));
                fitting.setIsReturn(jsonArray.getJSONArray(i).getString(5));
                fitting.setProductId(jsonArray.getJSONArray(i).getString(6));
                fitting.setProductName(jsonArray.getJSONArray(i).getString(7));
                fitting.setIsRetail(jsonArray.getJSONArray(i).getString(8));
                System.err.println(fitting);
                fittingProducts.add(fitting);
            }


        }


        return fittingProducts;
    }

    private MultiValueMap Data(String fittingId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'M_PRODUCT_ID;VALUE'");//
//            colums.add("'M_PRODUCT_ID'")//
        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE1'");//颜色
        colums.add("'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE2_CODE'");//尺寸
        colums.add("PRICELIST");//标准价
        colums.add("QTY");//数量
        colums.add("IS_RETURN");//归还状态
        colums.add("'M_PRODUCT_ID'");//id
        colums.add("'M_PRODUCT_ID;NAME'");//id
        colums.add("IS_RETAIL");//销售状态

        params.put("columns", colums);
        params.put("table", "M_FITTINGITEM");//表名
        params.put("count", "true");

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "BILLDATE");
//        orderby.put("asc", "false");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        HashMap paramss = new HashMap();
//        HashMap expr1 = new HashMap();
        paramss.put("column","'M_STORAGE_FREE_ID'");
        String ac = "'"+fittingId+"'";
        paramss.put("condition",ac);

//        HashMap expr2 = new HashMap();
//        expr2.put("column","BJ");
//        String state= null;
//        if(type.equals("1")){
//            state="'未回访'";
//        }else if(type.equals("2")){
//            state="'已回访'";
//        }
//
//        expr2.put("condition",state);
//
//        paramss.put("combine", "and");
//        paramss.put("expr1", expr1);
//        paramss.put("expr2", expr2);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetFittingItem g = new GetFittingItem("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("1019"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
